<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流水号" prop="recordNumber">
        <el-input
          v-model="queryParams.recordNumber"
          placeholder="请输入流水号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充电站" prop="recordStationId">
        <el-input
          v-model="queryParams.recordStationId"
          placeholder="请输入充电站"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充电枪id" prop="recordGunId">
        <el-input
          v-model="queryParams.recordGunId"
          placeholder="请输入充电枪id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充电度数" prop="recordAmount">
        <el-input
          v-model="queryParams.recordAmount"
          placeholder="请输入充电度数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充电金额" prop="recordMoney">
        <el-input
          v-model="queryParams.recordMoney"
          placeholder="请输入充电金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充电时长" prop="recordDuration">
        <el-input
          v-model="queryParams.recordDuration"
          placeholder="请输入充电时长"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单时间" prop="recordTime">
        <el-date-picker clearable
          v-model="queryParams.recordTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择订单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:yunyingrecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:yunyingrecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:yunyingrecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:yunyingrecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="yunyingrecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="充电记录id" align="center" prop="recordId" />
      <el-table-column label="流水号" align="center" prop="recordNumber" />
      <el-table-column label="充电站id" align="center" prop="recordStationId" />
      <el-table-column label="充电枪id" align="center" prop="recordGunId" />
      <el-table-column label="充电度数" align="center" prop="recordAmount" />
      <el-table-column label="充电金额" align="center" prop="recordMoney" />
      <el-table-column label="充电时长" align="center" prop="recordDuration" />
      <el-table-column label="订单时间" align="center" prop="recordTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.recordTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:yunyingrecord:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:yunyingrecord:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充电记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="yunyingrecordRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="流水号" prop="recordNumber">
          <el-input v-model="form.recordNumber" placeholder="请输入流水号" />
        </el-form-item>
        <el-form-item label="充电站id" prop="recordStationId">
          <el-input v-model="form.recordStationId" placeholder="请输入充电站id" />
        </el-form-item>
        <el-form-item label="充电枪id" prop="recordGunId">
          <el-input v-model="form.recordGunId" placeholder="请输入充电枪id" />
        </el-form-item>
        <el-form-item label="充电度数" prop="recordAmount">
          <el-input v-model="form.recordAmount" placeholder="请输入充电度数" />
        </el-form-item>
        <el-form-item label="充电金额" prop="recordMoney">
          <el-input v-model="form.recordMoney" placeholder="请输入充电金额" />
        </el-form-item>
        <el-form-item label="充电时长" prop="recordDuration">
          <el-input v-model="form.recordDuration" placeholder="请输入充电时长" />
        </el-form-item>
        <el-form-item label="订单时间" prop="recordTime">
          <el-date-picker clearable
            v-model="form.recordTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择订单时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
       <div style="display:flex;flex-wrap: wrap;height: 1500px">
         <div style="display:flex;flex-direction: column;width: 200px;">
           <div class="a">
             <div>充电费：</div>
             <div style="margin-top: -14px">{{totalcdMoney}}</div>
           </div>
           <div class="a">
             <div>服务费：</div>
             <div style="margin-top: -14px">{{totalserviceMony}}</div>
           </div>
           <div class="a">
             <div>营收合计：</div>
             <div style="margin-top: -14px">{{totalyingshouMoney}}</div>
           </div>
         </div>
         <div style="display:flex;flex-direction: column;width: 200px;margin-left: 20px">
         <div class="a">
           <div>充电度数：</div>
           <div style="margin-top: -14px">{{chargingmouthData}}</div>
         </div>
         <div class="a">
           <div>充电时长：</div>
           <div style="margin-top: -14px">{{chargingmouthDuration}}</div>
         </div>
         <div class="a">
           <div>充电次数：</div>
           <div style="margin-top: -14px">{{chargingmouthCount}}</div>

<!--           const chargingmouthData = ref([])-->
<!--           //月冲时长-->
<!--           const chargingmouthDuration = ref([])-->
<!--           //月冲次数-->
<!--           const chargingmouthCount = ref([])-->
         </div>
         </div>

           <div style="width: 600px;height: 100px;margin-left: 150px"><e-charts class="chart" :option="option" /></div>
           <div style="width: 600px;height: 100px;margin-left: 50px"><e-charts class="chart" :option="option3" /></div>
           <div style="width: 600px;height: 100px;margin-left: 50px"><e-charts class="chart" :option="option2" /></div>
           <div style="width: 800px;height: 150px;margin-left:50px"><e-charts class="chart" :option="option1" /></div>


       </div>
  </div>
</template>

<script setup name="Yunyingrecord">
import { listYunyingrecord,selecttotal,selectalls, getChartData,getYunyingrecord, delYunyingrecord, addYunyingrecord, updateYunyingrecord } from "@/api/system/yunyingrecord"
import { ref, reactive, computed, watch } from 'vue';
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    recordNumber: null,
    recordStationId: null,
    recordGunId: null,
    recordAmount: null,
    recordMoney: 0,
    recordDuration: null,
    recordTime: null,
  },
  lists:{
    charging_date: null,
    total_charging_amount: null,
    total_charging_money: null
  },
  rules: {
  }
})
const listAll = ref([]);

const { proxy } = getCurrentInstance()
//
const listChargeMoney = ref([]);
const listServiceMoney = ref([]);
const totalyingshouMoney = ref([])
const totalserviceMony = ref([])
const totalcdMoney = ref([])
const yunyingrecordList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const listst = ref([])

//月冲度数
const chargingmouthData = ref([])
//月冲时长
const chargingmouthDuration = ref([])
//月冲次数
const chargingmouthCount = ref([])

// 支付类型
const payType = ref([])
// 8月充电次数-日期
const totalDate = ref([])
const totalDate1 = ref([])
// 8月充电次数-数据
const totalData = ref([])
const totalData1 = ref([])
// 8月充电度数
const totalRechargeAmount = ref([])
// 8月充电时长
const totalRechargeTime = ref([])
const list = ref([])
// 图表配置
const option3 = ref({
  tooltip: {
    trigger: 'item'
  },
  legend: {
    top: '5%',
    left: 'center'
  },
  series: [{
    name: '支付方式',
    type: 'pie',
    radius: ['40%', '60%'],
    avoidLabelOverlap: false,
    itemStyle: {
      borderRadius: 10,
      borderColor: '#fff',
      borderWidth: 2
    },
    label: {
      show: false,
      position: 'center'
    },
    emphasis: {
      label: {
        show: true,
        fontSize: 18,
        fontWeight: 'bold'
      }
    },
    labelLine: {
      show: false
    },
    data: payType
  }]
})

const option2 = ref({
  title: {
    text: '8月营收总额'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  toolbox: {
    feature: {
      saveAsImage: {}
    }
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: list
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: '充电金额',
      type: 'line',
      data: listChargeMoney
    },
    {

      name: '服务费',
      type: 'line',
      data: listServiceMoney
    },

  ]
});
const option = ref({
  title: {
    text: '充电情况'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    data: ['充电度数', '充电时长']
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: totalDate
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: '充电度数',
      type: 'line',
      smooth: true,
      data: totalRechargeAmount
    },
    {
      name: '充电时长',
      type: 'line',
      smooth: true,
      data: totalRechargeTime
    }
  ]
})

const option1 = ref({
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      // Use axis to trigger tooltip
      type: 'shadow' // 'shadow' as default; can also be 'line' or 'shadow'
    }
  },
  legend: {},
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: {
    type: 'value'
  },
  yAxis: {
    type: 'category',
    data: totalDate
  },
  series: [
    {
      name: 'Direct',
      type: 'bar',
      stack: 'total',
      label: {
        show: true
      },
      emphasis: {
        focus: 'series'
      },
      data: totalData
    },
  ]
});



const { queryParams, form, rules } = toRefs(data)

onMounted(()=>{
  getList()
  getListall()
  getrecordlist()
  getweixinlis()
})

/** 查询充电记录列表 */
function getList() {
  loading.value = true
  listYunyingrecord(queryParams.value).then(response => {
    yunyingrecordList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

function getweixinlis(){
  selecttotal().then(response =>{
    console.log(8888999777)
    console.log(response)
    payType.value = response.map(item => item.pay_count)

  })
}
function getrecordlist(){
  selectalls().then(response =>{
    console.log(9999888)

    console.log(123456789)
    listst.value = response
    console.log(response)
    totalDate.value = listst.value.map(item => item.scharging_date)
    totalData.value =listst.value.map(item => item.scharging_count)
    totalRechargeAmount.value =listst.value.map(item => item.stotal_charging_amount)
    totalRechargeTime.value = listst.value.map(item => item.stotal_charging_duration)




    console.log(totalRechargeTime.value)
    //月冲度数
     chargingmouthData.value = listst.value.reduce((sum,item)=>sum+item.stotal_charging_amount,0)
    //月冲时长
     chargingmouthDuration.value = listst.value.reduce((sum,item)=>sum+item.stotal_charging_duration,0)
    //月冲次数
   chargingmouthCount.value = listst.value.reduce((sum,item)=>sum+item.scharging_count,0)
 })
}

function getListall() {
  getChartData().then(response => {
    console.log(8888)
    console.log(response)
    listAll.value = response
    list.value = listAll.value.map(item => item.charging_date)
    listChargeMoney.value = listAll.value.map(item => item.total_charging_money);
    listServiceMoney.value = listAll.value.map(item => (item.total_charging_money - item.total_charging_amount * 1.2).toFixed(2));
    totalyingshouMoney.value = listAll.value.reduce((sum, item) => sum + item.total_charging_money, 0).toFixed(2);
    totalcdMoney.value = listAll.value.reduce((sum,item) =>((sum+ item.total_charging_amount * 1.2)),0).toFixed(2);
    totalserviceMony.value = (totalyingshouMoney.value-totalcdMoney.value).toFixed(2);
    console.log(listAll.value.map(item => item.total_charging_money))

  })
}


// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    recordId: null,
    recordNumber: null,
    recordStationId: null,
    recordGunId: null,
    recordAmount: null,
    recordMoney: null,
    recordDuration: null,
    recordTime: null
  }
  proxy.resetForm("yunyingrecordRef")
}

/** 搜索按钮操作 */
function


handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
  // getrecordlist()
  // getweixinlis()

}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()

}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.recordId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加充电记录"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _recordId = row.recordId || ids.value
  getYunyingrecord(_recordId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改充电记录"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["yunyingrecordRef"].validate(valid => {
    if (valid) {
      if (form.value.recordId != null) {
        updateYunyingrecord(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addYunyingrecord(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _recordIds = row.recordId || ids.value
  proxy.$modal.confirm('是否确认删除充电记录编号为"' + _recordIds + '"的数据项？').then(function() {
    return delYunyingrecord(_recordIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/record1/export', {
    ...queryParams.value
  }, `yunyingrecord_${new Date().getTime()}.xlsx`)
}









</script>
<style>
.chart {

  height: 300px;
}
.a{
  margin-top: 20px;
  height: 80px;
  text-align: center;
  line-height: 50px;
  background: linear-gradient(to right, rgba(0, 0, 255, 0.5), rgba(0, 128, 255, 0.5));
}
</style>
